﻿using System;
using Airborne.ScrumPoker.Communication;
using Airborne.ScrumPoker.Model.TeamFoundation;
using Airborne.ScrumPoker.Module.Events;

namespace Airborne.ScrumPoker.Module.Client.Model
{
    public class ScrumPokerClientCallback : IScrumPokerCallback
    {

        #region StartTimer

        public event EventHandler<GenericEventArgs<int>> StartTimer;

        private void RaiseStartTimer(int seconds)
        {
            GenericEventArgs<int> args = new GenericEventArgs<int>(seconds);
            OnStartTimer(args);
        }

        protected virtual void OnStartTimer(GenericEventArgs<int> args)
        {
            EventHandler<GenericEventArgs<int>> handler = this.StartTimer;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region StopTimer

        public event EventHandler<EventArgs> StopTimer;

        private void RaiseStopTimer()
        {
            EventArgs args = new EventArgs();
            OnStopTimer(args);
        }

        protected virtual void OnStopTimer(EventArgs args)
        {
            EventHandler<EventArgs> handler = this.StopTimer;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region WorkItemSelected

        public event EventHandler<GenericEventArgs<ITeamWorkItem>> WorkItemSelected;

        private void RaiseWorkItemSelected(ITeamWorkItem detail)
        {
            GenericEventArgs<ITeamWorkItem> args = new GenericEventArgs<ITeamWorkItem>(detail);
            OnWorkItemSelected(args);
        }

        protected virtual void OnWorkItemSelected(GenericEventArgs<ITeamWorkItem> args)
        {
            EventHandler<GenericEventArgs<ITeamWorkItem>> handler = this.WorkItemSelected;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region EndGame

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "EndGame", Justification = "Is in fact a compound word")]
        public event EventHandler<EventArgs> EndGame;

        private void RaiseEndGame()
        {
            EventArgs args = EventArgs.Empty;
            OnEndGame(args);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "EndGame", Justification = "Is in fact a compound word")]
        protected virtual void OnEndGame(EventArgs args)
        {
            EventHandler<EventArgs> handler = this.EndGame;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region VoteReset

        public event EventHandler<EventArgs> VoteReset;

        private void RaiseVoteReset()
        {
            EventArgs args = EventArgs.Empty;
            OnVoteReset(args);
        }

        protected virtual void OnVoteReset(EventArgs args)
        {
            EventHandler<EventArgs> handler = this.VoteReset;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region IScrumPokerCallback Members

        public void TimerStarted(int seconds)
        {
            RaiseStartTimer(seconds);
        }

        public void TimerStopped()
        {
            RaiseStopTimer();
        }


        public void SelectWorkItem(string title, string description)
        {
            RaiseWorkItemSelected(new SimpleWorkItem() { Title = title, Description = description });
        }

        public void ResetVote()
        {
            RaiseVoteReset();
        }

        public void GameEnded()
        {
            RaiseEndGame();
        }
        #endregion
    }
}
